package imper.graphic.view;

import java.util.ArrayList;
import java.util.List;

import imper.engine.entity.Country;
import imper.engine.entity.Resources;
import imper.engine.entity.ResourcesType;
import imper.engine.util.ParameterManager;
import imper.engine.util.ResourceManager;
import imper.graphic.ImageManager;
import imper.graphic.listener.AddTransportRealListener;
import imper.graphic.listener.RemoveTransportRealListener;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.ProgressBar;

import TWLSlick.RootPane;

public class TransportView extends BarMenuView
{
	private List<Button> _transportbutton = null;
	
	private List<ProgressBar> _transportBar = null;
	
	@Override
	public void init( GameContainer pGameContainer , StateBasedGame pStateBasedGame ) throws SlickException
	{
		super.init(pGameContainer, pStateBasedGame);
		//
		_transportbutton = new ArrayList<Button>();
		//
		_transportBar = new ArrayList<ProgressBar>();

	}

	@Override
	public void render( GameContainer pGameContainer , StateBasedGame pStateBasedGame , Graphics pGraphics ) throws SlickException
	{
		Country country = ParameterManager.getInstance().getMainCountry();
		int nbRes = 0;
		ResourceManager transportCalc = country.getResourceTransportCalc();
		ResourceManager transportReal = country.getResourceTransportReal();
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			Image image = ImageManager.getInstance().getImage(ImageManager.RESOURCE, resourceType.getId());
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) && image != null)
			{
				int calcY = getYByRes( nbRes );
				int calcX = getXByRes( nbRes );
				pGraphics.drawImage(image, calcX, calcY);
				// ecriture des valeurs
				Resources resourceCalc = transportCalc.getResources(resourceType.getId());
				Resources resourceReal = transportReal.getResources(resourceType.getId());
				pGraphics.drawString(resourceReal.getValue() + "/" + resourceCalc.getValue(), calcX + 100, calcY + 22);
				nbRes++;
			}
		}
	}

	@Override
	public void update( GameContainer arg0 , StateBasedGame arg1 , int arg2 ) throws SlickException
	{
	}

	@Override
	protected void setViewRootPane(RootPane pRootPane)
	{
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) )
			{
				Button btnMore = new Button("+");
				btnMore.addCallback(new AddTransportRealListener(resourceType,this));
				pRootPane.add(btnMore);
				_transportbutton.add(btnMore);		

				Button btnLess = new Button("-");
				btnLess.addCallback(new RemoveTransportRealListener(resourceType,this));
				pRootPane.add(btnLess);
				_transportbutton.add(btnLess);				
				
				ProgressBar progressBar = new ProgressBar();
				pRootPane.add(progressBar);
				_transportBar.add(progressBar);	
			}
		}

	}

	public void calculateValue()
	{
		Country country = ParameterManager.getInstance().getMainCountry();
		ResourceManager transportCalc = country.getResourceTransportCalc();
		ResourceManager transportReal = country.getResourceTransportReal();
		
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) )
			{
				ProgressBar progressBar =_transportBar.get(resourceType.getId());
				Resources resourceCalc = transportCalc.getResources(resourceType.getId());
				Resources resourceReal = transportReal.getResources(resourceType.getId());
				float pour = 0.0f;
				pour = ((float)resourceReal.getValue()) / ((float)resourceCalc.getValue());
				progressBar.setValue(pour);//TODO
			}
		}

	}

	
	@Override
	protected void setViewLayout()
	{
		int nb = 0;
		int nbRes = 0;
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) && nb + 1 < _transportbutton.size())
			{
				int calcY = getYByRes( nbRes );
				int calcX = getXByRes( nbRes ) + 40;
				
				Button butMore = _transportbutton.get(nb++);
				butMore.adjustSize();
				butMore.setSize(20, 20);
				butMore.setPosition(calcX + 120, calcY);
				
				Button butLess = _transportbutton.get(nb++);
				butLess.adjustSize();
				butLess.setSize(20, 20);
				butLess.setPosition(calcX + 0, calcY);
				
				ProgressBar progressBar =_transportBar.get(nbRes);
				progressBar.adjustSize();
				progressBar.setSize(100, 20);
				progressBar.setPosition(calcX + 20, calcY);
				
				nbRes++;
			}
		}
	}
	
	private int getXByRes( int pNbRes)
	{
		return 40 + ( (pNbRes % 3) * 220 );
	}
	
	private int getYByRes( int pNbRes)
	{
		return 50 + ( (pNbRes / 3) * 60 ) ;
	}
	
	@Override
	public int getID()
	{
		return EnumView.TRANSPORTVIEW.getId();
	}

	
	
}
